package com.sskj.hhrt.message;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.sskj.base.BuildConfig;
import com.sskj.base.adapter.BaseAdapter;
import com.sskj.base.adapter.ViewHolder;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.DividerLineItemDecoration;
import com.sskj.hhrt.MVPFragment;
import com.sskj.hhrt.R;
import com.sskj.hhrt.WebActivity;
import com.sskj.hhrt.message.data.MessageData;

import butterknife.BindView;

/**
 * 平台公告
 *
 * @author Hey
 * Create at  2019/03/21
 */
public class NoticeFragment extends MVPFragment<NoticePresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;

    BaseAdapter<MessageData.Message> adapter;
    int page = 1;
    int pageSize = 10;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_notice;
    }

    @Override
    public NoticePresenter getPresenter() {
        return new NoticePresenter();
    }

    @Override
    public void initView() {
        initRefreshLayout(smartRefreshLayout);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth(2)
                .setFirstDraw(false)
                .setDividerColor(color(R.color.lineColor))
        );
        adapter = new BaseAdapter<MessageData.Message>(R.layout.item_message, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, MessageData.Message item) {
                holder.setText(R.id.message_title, item.getTitle())
                        .setText(R.id.message_time, item.getCreate_time());

                RequestOptions requestOptions = new RequestOptions()
                        .error(R.mipmap.icon_default)
                        .placeholder(R.mipmap.icon_default);
                Glide.with(getContext()).load(BuildConfig.BASE_URL + item.getImgCover()).apply(requestOptions).into((ImageView) holder.getView(R.id.message_img));
                ClickUtil.click(holder.getView(R.id.itemLayout), view -> {
                    WebActivity.start(getContext(), item.getId() + "", 2);
                });
            }
        };
    }


    @Override
    public void initData() {
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                mPresenter.getNoticeList(getAccount(), page, pageSize, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getNoticeList(getAccount(), page, pageSize, false);
            }
        });
    }

    @Override
    public void lazyLoad() {
        mPresenter.getNoticeList(getAccount(), page, pageSize, true);
    }

    public void setData(MessageData data) {
        if (data != null) {
            if (page == 1) {
                adapter.setNewData(data.getData());
            } else {
                adapter.addData(data.getData());
            }
        } else {
            adapter.setNewData(null);
        }
    }

    public void finishRefresh() {
        smartRefreshLayout.finishLoadMore();
        smartRefreshLayout.finishRefresh();
    }

    public static NoticeFragment newInstance() {
        NoticeFragment fragment = new NoticeFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


}
